package com.bw.parkingspace_module.parkingedit

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.bw.parkingspace_module.parkingedit.entity.*
import com.kf.common.entity.BaseEntity
import com.kf.common.entity.EventBusEntity
import com.kf.common.entity.EventBusType
import com.kf.common.mvi.viewmodel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.greenrobot.eventbus.EventBus
import java.io.File

class ParkingEditViewModel : BaseViewModel<ParkingEditModel, ParkingEditState>() {
    var list: ArrayList<ColorEntity> = ArrayList()
    var listDept: ArrayList<DeptEntity> = ArrayList()
    override fun createState(): ParkingEditState {
        return ParkingEditState(
            BaseEntity(0, "", list),
            BaseEntity(0, "", listDept),
        )
    }

    override fun createModel(): ParkingEditModel {
        return ParkingEditModel()
    }

    //vm层请求颜色方法
    fun requestColor() {
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestColor()
                .flowOn(Dispatchers.IO)
                .collect {
                    mutableContainer.updateUiState {
                        copy(colorEntity = it)
                    }
                }
        }
    }

    //部门方法
    fun requestDept() {
        var dept = SPUtils.getInstance().getInt("dept", -1)
        var map = HashMap<String, Any>()
        map.put("departmentId", dept)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestDept(map)
                .flowOn(Dispatchers.IO)
                .collect {
                    mutableContainer.updateUiState {
                        copy(deptEntity = it)
                    }
                }
        }
    }

    //添加车辆方法
    fun requestCar(
        carEntity: CarEntity,
        data: ArrayList<ImageEntity>,
        parkingEditEntity: ParkingEditEntity
    ) {
        var map: HashMap<String, Any> = HashMap()
        map.put("carNum", carEntity.carNum)
        map.put("colorId", carEntity.colorId)
        map.put("parkId", carEntity.parkId)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestCar(map).flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        var carId = it.values!!.carId
                        parkingEditEntity.carId = carId
                        if (data.size == 0) {
                            //请求添加车位接口
                            requestParking(parkingEditEntity)
                        } else {
                            for (i in data.indices) {
                                data.get(i).carId = carId
                            }
                            uploadImg(data)
                            requestCarImage(data, parkingEditEntity)
                        }
                    }
                }
        }
    }

    var upLoadindex = 0
    private fun uploadImg(data: ArrayList<ImageEntity>) {
        val file = File(data.get(upLoadindex++).imgPath)
        val body = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        val part = MultipartBody.Builder().addFormDataPart("file", file.name, body).build().parts()
        viewModelScope.launch(Dispatchers.IO) {
            mModel.upLoadCarImg(part).flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        LogUtils.e(it.msg)
                        if (upLoadindex < data.size) {
                            uploadImg(data)
                        }
                    }
                }
        }
    }

    //上传车辆图片方法
    private fun requestCarImage(
        data: ArrayList<ImageEntity>,
        parkingEditEntity: ParkingEditEntity
    ) {
        var map = HashMap<String, Any>()
        map.put("imgs", data)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestCarImage(map)
                .flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        requestParking(parkingEditEntity)
                    }
                }
        }
    }

    //申请车位方法
    fun requestParking(parkingEditEntity: ParkingEditEntity) {
        var map = HashMap<String, Any>()
        map.put("carId", parkingEditEntity.carId)
        map.put("parkEnd", parkingEditEntity.parkEnd)
        map.put("parkId", parkingEditEntity.parkId)
        map.put("parkName", parkingEditEntity.parkName)
        map.put("parkStart", parkingEditEntity.parkStart)
        map.put("parkTime", parkingEditEntity.parkTime)
        map.put("parkUser", parkingEditEntity.parkUser)
        viewModelScope.launch(Dispatchers.IO) {
            mModel.requestPaakingEdit(map)
                .flowOn(Dispatchers.IO)
                .collect {
                    if (it.statuesCode == 200) {
                        var eventBusEntity = EventBusEntity(EventBusType.PARKING_SUCCESS)
                        EventBus.getDefault().post(eventBusEntity)
                    }
                }
        }
    }
}